package utility;

import java.util.ArrayList;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Maja
 */
public class CommaSeperatorDocument extends PlainDocument {

    public void insertString(int i, String input, AttributeSet a) throws BadLocationException {

//            String str = this.getText(0, getLength());
//            char[] charArray = str.toCharArray();
//            if(str.equals("0,00")){
//                if(!input.equals("0")){
//                  charArray[3] = str;
//                }
//            }
        if (input.matches("\\d")) {
            String str = this.getText(0, getLength()) + input;
            str.replace(",", "");
            ArrayList<String> stringList = new ArrayList<>();
            String[] stringArray = str.split("");
            for (int j = 0; j < stringArray.length; j++) {
                stringList.add(stringArray[j]);
            }
            if(stringList.size()>3 && stringList.get(0).equals("0")){
                stringList.remove(0);
            }
            boolean exists = false;
            for (int j = stringArray.length-1; j >= 0; j--) {
                if(!stringArray[j].equals(0)){
                    exists = true;
                }
            }
            if(!exists){
                
            }

            super.insertString(i, input, a);
        }
    }

}
